import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:pwd_keeper/common/utils.dart';
import 'package:pwd_keeper/global.dart';
import 'package:pwd_keeper/iconfont.dart';
import 'package:pwd_keeper/pages/components/form_text.dart';
import 'package:pwd_keeper/pages/components/iconpicker/icon_picker_builder.dart';
import 'package:pwd_keeper/manager/pwd_keeper_manager.dart';

class PwdKeeperDetailPage extends StatefulWidget {
  PwdKeeperDetailPage({Key key, @required this.pwdKeeper}) : super(key: key);
  final Map pwdKeeper;

  @override
  State<StatefulWidget> createState() => _PwdKeeperDetailPageState();
}

class _PwdKeeperDetailPageState extends State<PwdKeeperDetailPage> {
  @override
  Widget build(BuildContext context) {
    PwdKeeperManager _pwdKeeperManager = Provider.of<PwdKeeperManager>(context);
    return Scaffold(
      appBar: AppBar(
        elevation: 0,
        actions: <Widget>[
          Offstage(
            offstage: widget.pwdKeeper['id'] == null,
            child: IconButton(
              icon: Icon(IconFont.lajitong),
              tooltip: '删除',
              onPressed: () {
                Utils.showConfirmDialog(
                  context: context,
                  content: '确认删除该密码？',
                  showCancelButton: true,
                  confirmCallback: () {
                    _pwdKeeperManager.delete(
                      id: widget.pwdKeeper['id'].toString(),
                    );
                    Navigator.pop(context);
                  },
                );
              },
            ),
          )
        ],
        title: Text(
          widget.pwdKeeper['desc'],
          style: TextStyle(
            color: Global.fontColor,
            fontSize: 15,
          ),
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ),
      body: Container(
        padding: EdgeInsets.all(10),
        width: MediaQuery.of(context).size.width,
        height: MediaQuery.of(context).size.height,
        decoration: BoxDecoration(color: Global.backgroundColor),
        child: SingleChildScrollView(
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              IconPickerBuilder(
                iconData: widget.pwdKeeper['icon'],
                action: (newIcon) {
                  setState(() => {widget.pwdKeeper['icon'] = newIcon});
                  Navigator.pop(context);
                },
              ),
              formTitle('账号'),
              FormText(
                maxLine: 1,
                text: widget.pwdKeeper['account'],
                hintText: '账号',
                onChanged: (text) {
                  widget.pwdKeeper['account'] = text;
                },
              ),
              formTitle('密码'),
              FormText(
                maxLine: 1,
                text: widget.pwdKeeper['password'],
                hintText: '密码',
                password: true,
                onChanged: (text) {
                  widget.pwdKeeper['password'] = text;
                },
              ),
              formTitle('登录地址'),
              FormText(
                maxLine: 1,
                text: widget.pwdKeeper['link'],
                hintText: '登录地址（选填）',
                onChanged: (text) {
                  widget.pwdKeeper['link'] = text;
                },
              ),
              formTitle('账号描述'),
              FormText(
                maxLine: 4,
                text: widget.pwdKeeper['desc'],
                hintText: '账号描述（选填）',
                onChanged: (text) {
                  widget.pwdKeeper['desc'] = text;
                },
              ),
              SizedBox(height: 50),
              MaterialButton(
                elevation: 0,
                minWidth: MediaQuery.of(context).size.width,
                height: 45,
                color: Global.fontColor,
                textColor: Global.backgroundColor,
                child: new Text('保存'),
                onPressed: () {
                  if (widget.pwdKeeper['account'].toString() == '' ||
                      widget.pwdKeeper['password'].toString() == '') {
                    Utils.showConfirmDialog(
                      context: context,
                      content: '请填写账号和密码',
                    );
                    return;
                  }

                  if (widget.pwdKeeper['id'] != null) {
                    _pwdKeeperManager.update(
                      id: widget.pwdKeeper['id'].toString(),
                      name: widget.pwdKeeper['name'].toString(),
                      account: widget.pwdKeeper['account'].toString(),
                      password: widget.pwdKeeper['password'].toString(),
                      desc: widget.pwdKeeper['desc'].toString(),
                      link: widget.pwdKeeper['link'].toString(),
                      icon: widget.pwdKeeper['icon'].toString(),
                    );
                  } else {
                    _pwdKeeperManager.insert(
                      name: widget.pwdKeeper['name'].toString(),
                      account: widget.pwdKeeper['account'].toString(),
                      password: widget.pwdKeeper['password'].toString(),
                      desc: widget.pwdKeeper['desc'].toString(),
                      link: widget.pwdKeeper['link'].toString(),
                      icon: widget.pwdKeeper['icon'].toString(),
                    );
                  }

                  Navigator.pop(context);
                },
              ),
            ],
          ),
        ),
      ),
    );
  }
}

Widget formTitle(String title) {
  return Container(
    padding: EdgeInsets.fromLTRB(7, 20, 10, 7),
    child: Text(
      title,
      style: TextStyle(
        color: Global.fontColor,
        fontSize: 16,
        fontWeight: FontWeight.w600,
      ),
    ),
  );
}
